import { defineConfig } from 'vite'
import path from "path";
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
// command: serve or build
// mode: development or production
export default defineConfig(({ command, mode }) => {
  // 判断是开发模式还是生产模式，返回不同的配置
  // if (mode === 'development') {
  //   return {}
  // } else {
  //   return {}
  // }
  return {
    //本地运行时的路径
    base: "./",
    // 插件
    plugins: [vue(),],
    // 路径别名
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      }
    },
    // 代理服务器
    server: {
      proxy: {
        '/api': {
          target: 'https://www.guowei.fun',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""), // 将路径/api替换为空
        }
      }
    },
    // 打包配置
    build: {
      minify: 'terser',// 需要先安装 terser
      terserOptions: {
        compress: {
          drop_console: true,//去掉console
          drop_debugger: true,//去掉debugger
        },
      },
      rollupOptions: {// 打包配置
        output: {
          manualChunks(id) {// 手动分包
            if (id.includes('node_modules')) {
              return id
                .toString()
                .split('node_modules/')[1]
                .split('/')[0]
                .toString();
            }
          },
          chunkFileNames: (chunkInfo) => {// 分包文件名
            const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : [];
            const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]';
            return `js/${fileName}/[name].[hash].js`;
          }
        }
      }
    }
  }
})
